package com.hengtuo.student.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.hengtuo.student.dao.ClassesDao;
import com.hengtuo.student.dao.base.impl.BaseDaoImpl;
import com.hengtuo.student.pojo.Classes;
import com.hengtuo.student.pojo.User;
import com.hengtuo.student.pojo.UserClassesFuntion;
import com.hengtuo.student.util.Paginator;

@Repository("classesDaoImpl")
public class ClassesDaoImpl extends BaseDaoImpl<Classes> implements ClassesDao<Classes>{

	@Override
	public void unbind(Classes classes) {
		Query query=this.getSession().createQuery("from UserClassesFunction where classesid=:classId");
		query.setString("classId", classes.getId());
		List<UserClassesFuntion> userClassesFunctions=query.list();
		if(userClassesFunctions.size()!=0||userClassesFunctions!=null){
			for(UserClassesFuntion userClassesFunction:userClassesFunctions){
				this.getSession().delete(userClassesFunction);
			}}
	}

	@Override
	public Collection<User> getUsersByClass(String classId) {
		//from User where id = (select userid from UserClassesFuntion where classesid = classId)
		List<User> users = new ArrayList<User>();
		if(classId!=null){
			String hql = "from UserClassesFuntion where classesid=:classId";
			String hqlUser ="from User where id =:userId";
			Query query =this.getSession().createQuery(hql);
			query.setString("classId", classId);
			List<UserClassesFuntion> ucfl = query.list();
			if(ucfl!=null&&ucfl.size()!=0){
				for(UserClassesFuntion ucfl1:ucfl){
					Query queryUser=this.getSession().createQuery(hqlUser);
					queryUser.setString("userId", ucfl1.getUserid());
					User user =	(User) queryUser.list().get(0);
					users.add(user);
				}
			}
		}
		return users;
	}

	@Override
	public Collection<Classes> queryClassesByUser(String userId) {
		String hql ="from UserClassesFuntion where userid=:userId";
		Query query =this.getSession().createQuery(hql);
		query.setString("userId", userId);
		List<UserClassesFuntion> userClassesFuntionsList=query.list();
		if (userClassesFuntionsList!=null) {
			UserClassesFuntion userClassesFuntion=	userClassesFuntionsList.get(0);
			String classId=userClassesFuntion.getClassesid();
			String hqlClss ="from Classes where id=:classId";
			Query queryClass =this.getSession().createQuery(hqlClss);
			queryClass.setString("classId",classId);
			List<Classes> classesList=queryClass.list();
			return classesList;
		}else {
			return null;
		}

	}

	@Override
	public void getAllClassesPaginator(Paginator<User> p) {
		String countsql ="select count(id) from tbl_class";
		BigInteger count = (BigInteger) this.getSession().createSQLQuery(countsql).uniqueResult();
		p.setTotalCount(count.longValue());
		String hql = "from Classes";
		Query q=this.getSession().createQuery(hql);
		q.setFirstResult(p.getStartResult());
		q.setMaxResults(p.getPageSize());
		p.setDataList(q.list());
	}
}
